package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.annotaion.ApiMethod;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.response.ArrivalRate;
import com.xmy.cultivate.entity.response.MathChaPlan;
import com.xmy.cultivate.entity.response.PassRade;
import com.xmy.cultivate.entity.views.ChaPlan;
import com.xmy.cultivate.entity.views.FenAdminData;
import com.xmy.cultivate.entity.views.LeadDataRs;
import com.xmy.cultivate.feign.math_outside.IMathematicsFeignClient;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/math")
public class MathController extends BaseController {
    @Autowired
    IMathematicsFeignClient iMathematicsFeignClient;

    @Autowired
    IStudentService iStudentService;

    @Autowired
    IGradeService iGradeService;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IStaffService iStaffService;

    @Autowired
    IOrganizationService iOrganizationService;

    @Autowired
    ICourseTypeService iCourseTypeService;

    @Autowired
    IRecordCourseService iRecordCourseService;

    @Autowired
    ISchoolQuarterService iSchoolQuarterService;

    @Autowired
    IAuthAdminContentService iAuthAdminContentService;


    /**
     * 数据录入列表
     *
     * @param pageNum
     * @param pageSize
     * @param schoolId
     * @param yearPart
     * @param quarterNum
     * @param teacherId
     * @param gradeId
     * @param yearClassId
     * @param gradeSection
     * @param classTypeId
     * @param lessonNum
     * @param studentName
     * @param status
     * @param subjectsId
     * @return
     */
    @GetMapping("/getChaPanAddList")
    public Result getChaPanAddList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "1000000") Integer pageSize,
                                   @RequestParam(value = "schoolId", required = false) String schoolId,
                                   @RequestParam(value = "yearPart", required = false) String yearPart,
                                   @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                   @RequestParam(value = "teacherId", required = false) String teacherId,
                                   @RequestParam(value = "gradeId", required = false) String gradeId,
                                   @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                   @RequestParam(value = "gradeSection", required = false) String gradeSection,
                                   @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                   @RequestParam(value = "lessonNum", required = false) String lessonNum,
                                   @RequestParam(value = "studentName", required = false) String studentName,
                                   @RequestParam(value = "status", required = false) String status,
                                   @RequestParam(value = "subjectsId", required = false, defaultValue = "2") String subjectsId,
                                   @RequestParam(value = "count", required = false) String count,
                                   @RequestParam(value = "categoryId", defaultValue = "1", required = false) Integer categoryId) {
        if (StringUtils.isBlank(lessonNum)) {
            return new Result(Code.OK, "无数据", "");
        }


        Quarter quarterNow = iQuarterService.getNowQuarter();
        yearPart = quarterNow.getYearPart().toString();
        quarterNum = quarterNow.getNum().toString();


        //暂时教学教师不能录入数据
        QueryWrapper<Grade> gradeQueryWrapper = new QueryWrapper<>();
        gradeQueryWrapper.eq("year_part", yearPart);
        gradeQueryWrapper.eq("quarter_num", quarterNum);
        gradeQueryWrapper.eq("lesson_type", 1);
        gradeQueryWrapper.eq("teacher_id", this.adminId);
        Long counts = iGradeService.count(gradeQueryWrapper);
        if (counts > 0) {
            Page<MathChaPlan> iPageNo = new Page<>(pageNum, pageSize);
            return new Result(Code.OK, new PageResult(iPageNo.getRecords(), iPageNo.getTotal(), iPageNo.getPages()));
        }


        String schoolIdStr = schoolId;
        if (StringUtils.isBlank(studentName)) {
            //return new Result(Code.ERROR,"","请输入姓名搜索");
        }

        if (StringUtils.isBlank(schoolId)) {
            schoolIdStr = this.baseSchoolIdStr;
        }
        if (StringUtils.isNotBlank(gradeSection)) {
            yearClassId = CommonUtil.getYearClassIdsForGradeSection(Integer.parseInt(gradeSection));
        }

        Page<MathChaPlan> iPage = iMathematicsFeignClient.getChaPanAddList(pageNum, pageSize, schoolIdStr, yearPart, quarterNum, teacherId, gradeId, yearClassId, gradeSection, classTypeId, lessonNum, studentName, status, subjectsId, count, categoryId);

        /*for (MathChaPlan mathChaPlan:iPage.getRecords()){
            Student student = iStudentService.getById(mathChaPlan.getStudentId());
            if (student!=null){
                mathChaPlan.setStudentName(student.getName());
            }
            Grade grade = iGradeService.getById(mathChaPlan.getGradeId());
            if (grade !=null){
                mathChaPlan.setGradeName(grade.getName());
            }
        }*/

        //return new Result(Code.OK,new ,"");

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    @PostMapping("/addChaPlan")
    public Result addChaPlan(@RequestBody MathChaPlan mathChaPlan) {
        Long createdId = this.adminId;
        mathChaPlan.setCreatedId(createdId);
        boolean rs = iMathematicsFeignClient.addChaPlan(mathChaPlan);
        if (rs) {
            return new Result(Code.OK, "", "保存成功");
        } else {
            return new Result(Code.ERROR, "", "保存失败");
        }

    }

    /**
     * 数学通关率
     *
     * @param schoolId             校区id
     * @param yearPart             年份
     * @param quarterNum           学期
     * @param type                 1个人，2校区老师，3校长，4校区，5战队，6四大校区
     * @param lessonNumStart       开始课次
     * @param lessonNum            结束课次
     * @param yearClassId          年级 0~12
     * @param gradeSection         学段，0：少儿（0~2年级），1：小学（3~6年级），2中学（7~9年级），3：高中（10~12年级）
     * @param classTypeId          班型
     * @param readingType          1：不包含（在读、结班状态的学员），2，包含（在读、结班状态的学员，或退费、停课并且使用课次大于0的学员）
     * @param passDate
     * @param passDateStart        开始日期
     * @param passDateEnd          结束日期
     * @param subjectsId           科目
     * @param rightNumStartDateGet 正确数开始日期
     * @param rightNumEndDateGet   正确数结束日期
     * @param trackId              赛道id
     * @param listState            是否根据班级分组，0：否，1：是
     * @param radeType             1：通关率，2：正确率，3：修正率
     * @param teacherId            老师id
     * @param name                 老师姓名
     * @param classify             赛道分类id
     * @param trackType            赛道分类，1：市场部，2：战队
     * @param teamId               战队id
     * @param include              是否排除学员
     * @param inType               1：全部数据，2：已录入的数据
     * @param categoryId           区分是否为考前冲刺类型的数据，1：正常通关率，3：考前冲刺通关率
     * @param isAchieve            0通关率、录入、完成率、正确率、修正率，1复刷完成率
     */
    @ApiMethod(value = "home:math:clearance-rate,home:math:correct-rate", description = "数学通关率,数学正确率")
    @GetMapping("/getPassRadeList")
    public Result getPassRadeList(@RequestParam(value = "schoolId", required = false) String schoolId,
                                  @RequestParam(value = "yearPart", required = false) String yearPart,
                                  @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                  @RequestParam(value = "type", required = false) String type,
                                  @RequestParam(value = "lessonNumStart", required = false, defaultValue = "0") String lessonNumStart,
                                  @RequestParam(value = "lessonNum", required = false) String lessonNum,
                                  @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                  @RequestParam(value = "gradeSection", required = false) String gradeSection,
                                  @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                  @RequestParam(value = "readingType", required = false, defaultValue = "1") String readingType,
                                  @RequestParam(value = "passDate", required = false) String passDate,
                                  @RequestParam(value = "passDateStart", required = false) String passDateStart,
                                  @RequestParam(value = "passDateEnd", required = false) String passDateEnd,
                                  @RequestParam(value = "subjectsId", required = false, defaultValue = "2") String subjectsId,
                                  @RequestParam(value = "rightNumStartDate", required = false) String rightNumStartDateGet,
                                  @RequestParam(value = "rightNumEndDate", required = false) String rightNumEndDateGet,
                                  @RequestParam(value = "trackId", required = false) String trackId,
                                  @RequestParam(value = "listState", required = false, defaultValue = "0") String listState,//0原来的列表；1安班级来列出
                                  @RequestParam(value = "radeType", required = false) String radeType,
                                  @RequestParam(value = "teacherId", required = false) String teacherId,
                                  @RequestParam(value = "name", required = false) String name,
                                  @RequestParam(value = "isYearClass", required = false, defaultValue = "0") Integer isYearClass, // 是否根据年级分组，0否，1是
                                  @RequestParam(value = "classify", required = false) String classify,
                                  @RequestParam(value = "trackType", required = false) String trackType,
                                  @RequestParam(value = "teamId", required = false) String teamId,
                                  @RequestParam(value = "include", required = false) String include,//0三次课以内，1三次课以外
                                  @RequestParam(value = "inType", required = false, defaultValue = "1") String inType,
                                  @RequestParam(value = "categoryId", defaultValue = "1", required = false) Integer categoryId,
                                  @RequestParam(value = "isAchieve", defaultValue = "0", required = false) String isAchieve, // 0通关率、录入、完成率、正确率、修正率，1复刷完成率
                                  @RequestParam(value = "moldType", required = false, defaultValue = "1") String moldType, // 1正常通关率、2到勤通关率
                                  @RequestParam(value = "examSchedulingtType", required = false, defaultValue = "0") String examSchedulingtType //  0全部 1已过刷题排班时间 2未到刷题排班时间
    ) {


        if (name == null || name.isEmpty()) {
            name = null;
        }

        if (StringUtils.isBlank(radeType)) {
            return new Result(Code.ERROR, "", "更新了页面，请刷新查看");
        }

        Quarter quarter = iQuarterService.getNowQuarter();
        String readingStatusStr = "1,5";//结课的
        String isBeforQuarter = "1";//是否之前学期1是，0否
        if (quarter != null) {
            //当前学期
            if (yearPart.equals(quarter.getYearPart().toString()) && quarterNum.equals(quarter.getNum().toString())) {
                isBeforQuarter = "0";
                readingStatusStr = "1,5";
            }
        }

        Integer isIncludeFirstCourse = 0;
        if (quarter.getId() >= 18) {
            isIncludeFirstCourse = 1;
        }

        if (readingType.equals("2")) {
            readingStatusStr = "1,3,4,5";
        }
        if (StringUtils.isBlank(passDate)) {
            passDate = LocalDate.now().toString();
        }
        if (StringUtils.isBlank(passDateEnd)) {
            passDateEnd = LocalDate.now().toString();
        }

        String rightNumStartDate = "";
        String rightNumEndDate = "";
        if (rightNumStartDateGet != null && !rightNumStartDateGet.isEmpty()) {
            rightNumStartDate = rightNumStartDateGet + " 00:00:00";
        }
        if (rightNumEndDateGet != null && !rightNumEndDateGet.isEmpty()) {
            rightNumEndDate = rightNumEndDateGet + " 23:59:59";
        }


        AuthAdminContent authAdminContent = iAuthAdminContentService.getPermissionForTeacherId(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), this.adminId, this.baseSchoolIdStr);
        List<MathChaPlan> mathChaPlanList = new ArrayList<>();
        //个人
        if (authAdminContent.getIsPerson() == 1) {
            //如果type为1或者为2
            if (!type.equals("1") && !type.equals("2")) {
                return new Result(Code.OK, new PageResult(mathChaPlanList, (long) mathChaPlanList.size(), 1L));
            }
        }
        //不包含数学科目
        if (StringUtils.isBlank(authAdminContent.getSubjectsIds())) {
            //return new Result(Code.OK, new PageResult(mathChaPlanList, (long) mathChaPlanList.size(), 1L));
        }
        if (!authAdminContent.getSubjectsIds().contains("2")) {
            //return new Result(Code.OK, new PageResult(mathChaPlanList, (long) mathChaPlanList.size(), 1L));
        }


        String schoolIdStr = schoolId;
//        if (StringUtils.isBlank(schoolId)) {
//            if (type.equals("2")) {
//                //校区老师状态，只能看自己所在校区的数据
//                schoolIdStr = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
//            }
//        }
        if (StringUtils.isBlank(schoolId)) {
            //校区老师状态，只能看自己所在校区的数据
            schoolIdStr = authAdminContent.getSchoolIds();
        } else {
            schoolIdStr = iAuthAdminContentService.getIntersectionForStr(schoolId, authAdminContent.getSchoolIds());
            String leadSchoolIds = iAuthAdminContentService.getIntersectionForStr(schoolId, authAdminContent.getLeadDataRs().getSchoolIds());
            authAdminContent.getLeadDataRs().setSchoolIds(leadSchoolIds);
        }

        StringBuilder yearClassIdSb = new StringBuilder();
        String sectionYearClassId = "";
        if (StringUtils.isNotBlank(gradeSection)) {
            String[] sections = gradeSection.split(",");
            for (String section : sections) {
                if (section.equals("0")) {
                    yearClassIdSb.append("0,1,2");
                } else if (section.equals("1")) {
                    yearClassIdSb.append("3,4,5,6");
                } else if (section.equals("2")) {
                    yearClassIdSb.append("7,8,9");
                } else if (section.equals("3")) {
                    yearClassIdSb.append("10,11,12");
                }
                if (!section.equals(sections[sections.length - 1])) {
                    yearClassIdSb.append(",");
                }
            }
            sectionYearClassId = yearClassIdSb.toString();
            if (StringUtils.isBlank(yearClassId)) {
                yearClassId = authAdminContent.getYearClassIds();
                if (StringUtils.isNotBlank(yearClassId) && StringUtils.isNotBlank(sectionYearClassId)) {
                    yearClassId = CommonUtil.getStrIntersection(yearClassId, sectionYearClassId);
                }
            }
        }

        if (StringUtils.isBlank(yearClassId)) {
            yearClassId = authAdminContent.getYearClassIds();
        } else {
            String leadYearClassIds = iAuthAdminContentService.getIntersectionForStr(yearClassId, authAdminContent.getLeadDataRs().getYearClassIds());
            authAdminContent.getLeadDataRs().setYearClassIds(leadYearClassIds);
            yearClassId = iAuthAdminContentService.getIntersectionForStr(yearClassId, authAdminContent.getYearClassIds());
        }
        if (StringUtils.isNotBlank(yearClassId) && StringUtils.isNotBlank(sectionYearClassId)) {
            yearClassId = CommonUtil.getStrIntersection(yearClassId,sectionYearClassId);
        }

        if (StringUtils.isBlank(teacherId)) {
            if (authAdminContent.getIsPerson() == 1) {
                teacherId = String.valueOf(this.adminId);
            }
        }
        if (StringUtils.isBlank(subjectsId)) {
            subjectsId = authAdminContent.getSubjectsIds();
        } else {
            String leadSubjectsIds = iAuthAdminContentService.getIntersectionForStr(subjectsId, authAdminContent.getLeadDataRs().getSubjectsIds());
            authAdminContent.getLeadDataRs().setSubjectsIds(leadSubjectsIds);
            //subjectsId = iAuthAdminContentService.getIntersectionForStr(subjectsId, authAdminContent.getSubjectsIds());
        }

        Quarter quarterForYearAndNum = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        StringBuilder courseSchedulingDetailName = new StringBuilder();
        courseSchedulingDetailName.append("course_scheduling_detail");
        if (quarter.getId() != quarterForYearAndNum.getId()) {
            courseSchedulingDetailName.append("_");
            courseSchedulingDetailName.append(yearPart);
            courseSchedulingDetailName.append("_");
            courseSchedulingDetailName.append(quarterNum);
        }
        LeadDataRs leadDataRs = authAdminContent.getLeadDataRs();
        String leadSubjectsIds = leadDataRs.getSubjectsIds();
        String leadSchoolIds = leadDataRs.getSchoolIds();
        String leadYearClassIds = leadDataRs.getYearClassIds();
        Long leadTeacherId = leadDataRs.getTeacherId();

        mathChaPlanList = iMathematicsFeignClient.getPassRadeList(schoolIdStr, yearPart, quarterNum, type, lessonNumStart, lessonNum, yearClassId, classTypeId, readingType, readingStatusStr, isBeforQuarter, passDate, passDateStart, passDateEnd, subjectsId, rightNumStartDate, rightNumEndDate, trackId, isYearClass, radeType, listState, teacherId, name, classify, trackType, teamId, include, inType, categoryId, isAchieve, moldType, String.valueOf(courseSchedulingDetailName), examSchedulingtType, leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId, isIncludeFirstCourse);


        if (mathChaPlanList == null || mathChaPlanList.size() == 0) {
//            return new Result(Code.OK, new PageResult<>(new ArrayList<>(), 0L, 1L), "无数据");
            mathChaPlanList = new ArrayList<>();
            return new Result(Code.OK, new PageResult(mathChaPlanList, (long) mathChaPlanList.size(), 1L));
        }

//        if (type.equals("6") && StringUtils.isBlank(schoolId)) {
        if (type.equals("6")) {
            List<MathChaPlan> collectList = new ArrayList<>();

            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("year_part", yearPart);
            queryWrapper.eq("quarter_num", quarterNum);
            queryWrapper.inSql(StringUtils.isNotBlank(schoolId), "school_id", schoolIdStr);
            queryWrapper.eq("pid", 0);
            List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

            for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                MathChaPlan mathChaPlan = new MathChaPlan();
                mathChaPlan.setSchoolId(schoolQuarter.getSchoolId());
                mathChaPlan.setSchoolName(schoolQuarter.getSchoolName());
                mathChaPlan.setSchoolPid(schoolQuarter.getPid());
                mathChaPlan.setAllShutNum(0);
                mathChaPlan.setPlanShutNumPass(0);
                mathChaPlan.setNewNumAll(0);
                mathChaPlan.setLowestShutnumAll(0);
                mathChaPlan.setRightNumAll(0);
                mathChaPlan.setRightNumDate(0);
                mathChaPlan.setAmendNumAll(0);
                mathChaPlan.setErrorNumAll(0);
                mathChaPlan.setShowPassNum(0);
                mathChaPlan.setYetPassNum(0);
                mathChaPlan.setNotPassNum(0);
                mathChaPlan.setNotBrushNum(0);
                mathChaPlan.setAgainBrushNum(0);
                mathChaPlan.setDatePassNum(0);
                mathChaPlan.setYetFinishCount(0);
                mathChaPlan.setDateYetFinishCount(0);
                mathChaPlan.setRebrushCount(0);
                mathChaPlan.setYetRebrushCount(0);
                mathChaPlan.setDateYetRebrushCount(0);
                mathChaPlan.setAgainAmendNum(0);
                mathChaPlan.setYetFinishCount2(0);
                mathChaPlan.setDateYetFinishCount2(0);
                mathChaPlan.setAgainAmendNum2(0);
                mathChaPlan.setAgainBrushNum2(0);
                mathChaPlan.setNotAgainBrushCount(0);
                mathChaPlan.setYetAgainBrushCount(0);
                mathChaPlan.setDateYetAgainBrushCount(0);
                collectList.add(mathChaPlan);
            }

            for (MathChaPlan mathChaPlan : collectList) {
                List<MathChaPlan> childRates = new ArrayList<>();
                for (MathChaPlan chaPlan : mathChaPlanList) {
                    if (mathChaPlan.getSchoolId().equals(chaPlan.getSchoolId()) || mathChaPlan.getSchoolId().equals(chaPlan.getSchoolPid())) {
                        childRates.add(chaPlan);
                        if (chaPlan.getAllShutNum() != null) {
                            mathChaPlan.setAllShutNum(mathChaPlan.getAllShutNum() + chaPlan.getAllShutNum());
                        }
                        if (chaPlan.getPlanShutNumPass() != null) {
                            mathChaPlan.setPlanShutNumPass(mathChaPlan.getPlanShutNumPass() + chaPlan.getPlanShutNumPass());
                        }
                        if (chaPlan.getNewNumAll() != null) {
                            mathChaPlan.setNewNumAll(mathChaPlan.getNewNumAll() + chaPlan.getNewNumAll());
                        }
                        if (chaPlan.getLowestShutnumAll() != null) {
                            mathChaPlan.setLowestShutnumAll(mathChaPlan.getLowestShutnumAll() + chaPlan.getLowestShutnumAll());
                        }
                        if (chaPlan.getRightNumAll() != null) {
                            mathChaPlan.setRightNumAll(mathChaPlan.getRightNumAll() + chaPlan.getRightNumAll());
                        }
                        if (chaPlan.getRightNumDate() != null) {
                            mathChaPlan.setRightNumDate(mathChaPlan.getRightNumDate() + chaPlan.getRightNumDate());
                        }
                        if (chaPlan.getAmendNumAll() != null) {
                            mathChaPlan.setAmendNumAll(mathChaPlan.getAmendNumAll() + chaPlan.getAmendNumAll());
                        }
                        if (chaPlan.getErrorNumAll() != null) {
                            mathChaPlan.setErrorNumAll(mathChaPlan.getErrorNumAll() + chaPlan.getErrorNumAll());
                        }
                        if (chaPlan.getShowPassNum() != null) {
                            mathChaPlan.setShowPassNum(mathChaPlan.getShowPassNum() + chaPlan.getShowPassNum());
                        }
                        if (chaPlan.getYetPassNum() != null) {
                            mathChaPlan.setYetPassNum(mathChaPlan.getYetPassNum() + chaPlan.getYetPassNum());
                        }
                        if (chaPlan.getNotPassNum() != null) {
                            mathChaPlan.setNotPassNum(mathChaPlan.getNotPassNum() + chaPlan.getNotPassNum());
                        }
                        if (chaPlan.getNotBrushNum() != null) {
                            mathChaPlan.setNotBrushNum(mathChaPlan.getNotBrushNum() + chaPlan.getNotBrushNum());
                        }
                        if (chaPlan.getAgainBrushNum() != null) {
                            mathChaPlan.setAgainBrushNum(mathChaPlan.getAgainBrushNum() + chaPlan.getAgainBrushNum());
                        }
                        if (chaPlan.getDatePassNum() != null) {
                            mathChaPlan.setDatePassNum(mathChaPlan.getDatePassNum() + chaPlan.getDatePassNum());
                        }
                        if (chaPlan.getYetFinishCount() != null) {
                            mathChaPlan.setYetFinishCount(mathChaPlan.getYetFinishCount() + chaPlan.getYetFinishCount());
                        }
                        if (chaPlan.getDateYetFinishCount() != null) {
                            mathChaPlan.setDateYetFinishCount(mathChaPlan.getDateYetFinishCount() + chaPlan.getDateYetFinishCount());
                        }
                        if (chaPlan.getRebrushCount() != null) {
                            mathChaPlan.setRebrushCount(mathChaPlan.getRebrushCount() + chaPlan.getRebrushCount());
                        }
                        if (chaPlan.getYetRebrushCount() != null) {
                            mathChaPlan.setYetRebrushCount(mathChaPlan.getYetRebrushCount() + chaPlan.getYetRebrushCount());
                        }
                        if (chaPlan.getDateYetRebrushCount() != null) {
                            mathChaPlan.setDateYetRebrushCount(mathChaPlan.getDateYetRebrushCount() + chaPlan.getDateYetRebrushCount());
                        }
                        if (chaPlan.getAgainAmendNum() != null) {
                            mathChaPlan.setAgainAmendNum(mathChaPlan.getAgainAmendNum() + chaPlan.getAgainAmendNum());
                        }
                        if (chaPlan.getYetFinishCount2() != null) {
                            mathChaPlan.setYetFinishCount2(mathChaPlan.getYetFinishCount2() + chaPlan.getYetFinishCount2());
                        }
                        if (chaPlan.getDateYetFinishCount2() != null) {
                            mathChaPlan.setDateYetFinishCount2(mathChaPlan.getDateYetFinishCount2() + chaPlan.getDateYetFinishCount2());
                        }
                        if (chaPlan.getAgainAmendNum2() != null) {
                            mathChaPlan.setAgainAmendNum2(mathChaPlan.getAgainAmendNum2() + chaPlan.getAgainAmendNum2());
                        }
                        if (chaPlan.getAgainBrushNum2() != null) {
                            mathChaPlan.setAgainBrushNum2(mathChaPlan.getAgainBrushNum2() + chaPlan.getAgainBrushNum2());
                        }
                        if (chaPlan.getNotAgainBrushCount() != null) {
                            mathChaPlan.setNotAgainBrushCount(mathChaPlan.getNotAgainBrushCount() + chaPlan.getNotAgainBrushCount());
                        }
                        if (chaPlan.getYetAgainBrushCount() != null) {
                            mathChaPlan.setYetAgainBrushCount(mathChaPlan.getYetAgainBrushCount() + chaPlan.getYetAgainBrushCount());
                        }
                        if (chaPlan.getDateYetAgainBrushCount() != null) {
                            mathChaPlan.setDateYetAgainBrushCount(mathChaPlan.getDateYetAgainBrushCount() + chaPlan.getDateYetAgainBrushCount());
                        }
                    }
                }
                mathChaPlan.setChildRateList(childRates);
            }

            mathChaPlanList = collectList;
        }

        for (MathChaPlan mathChaPlan : mathChaPlanList) {
            Staff staff = iStaffService.getById(mathChaPlan.getTeacherId());
            if (staff != null) {
                mathChaPlan.setTeacherName(staff.getName());
            }
            Organization organization = iOrganizationService.getById(mathChaPlan.getSchoolId());
            if (organization != null) {
                mathChaPlan.setSchoolName(organization.getName());
            }
//            mathChaPlan.setArrivalRateList(iRecordCourseService.arrivalRate(String.valueOf(mathChaPlan.getSchoolId()), "2", Integer.valueOf(yearPart), Integer.valueOf(quarterNum), null, passDateStart, passDateEnd, Integer.valueOf(type), "", "", this.baseSchoolIdList, 3, yearClassId, trackId, null, "5", "2", null, name,String.valueOf(mathChaPlan.getTeacherId())));
        }

        return new Result(Code.OK, new PageResult<>(mathChaPlanList, (long) mathChaPlanList.size(), 1L));
    }


    @GetMapping("/getPassRadeListIFeign")
    public List<MathChaPlan> getPassRadeListIFeign(@RequestParam(value = "schoolId", required = false) String schoolId,
                                                   @RequestParam(value = "yearPart", required = false) String yearPart,
                                                   @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                                   @RequestParam(value = "type", required = false) String type,
                                                   @RequestParam(value = "lessonNumStart", required = false, defaultValue = "0") String lessonNumStart,
                                                   @RequestParam(value = "lessonNum", required = false) String lessonNum,
                                                   @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                                   @RequestParam(value = "gradeSection", required = false) String gradeSection,
                                                   @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                                   @RequestParam(value = "readingType", required = false, defaultValue = "1") String readingType,
                                                   @RequestParam(value = "passDate", required = false) String passDate,
                                                   @RequestParam(value = "passDateStart", required = false) String passDateStart,
                                                   @RequestParam(value = "passDateEnd", required = false) String passDateEnd,
                                                   @RequestParam(value = "subjectsId", required = false, defaultValue = "2") String subjectsId,
                                                   @RequestParam(value = "rightNumStartDate", required = false) String rightNumStartDateGet,
                                                   @RequestParam(value = "rightNumEndDate", required = false) String rightNumEndDateGet,
                                                   @RequestParam(value = "trackId", required = false) String trackId,
                                                   @RequestParam(value = "listState", required = false, defaultValue = "0") String listState,//0原来的列表；1安班级来列出
                                                   @RequestParam(value = "radeType", required = false) String radeType,
                                                   @RequestParam(value = "teacherId", required = false) String teacherId,
                                                   @RequestParam(value = "name", required = false) String name,
                                                   @RequestParam(value = "isYearClass", required = false, defaultValue = "0") Integer isYearClass, // 是否根据年级分组，0否，1是
                                                   @RequestParam(value = "classify", required = false) String classify,
                                                   @RequestParam(value = "trackType", required = false) String trackType,
                                                   @RequestParam(value = "teamId", required = false) String teamId,
                                                   @RequestParam(value = "include", required = false) String include,//0三次课以内，1三次课以外
                                                   @RequestParam(value = "inType", required = false, defaultValue = "1") String inType,
                                                   @RequestParam(value = "categoryId", defaultValue = "1", required = false) Integer categoryId,
                                                   @RequestParam(value = "isAchieve", defaultValue = "0", required = false) String isAchieve, // 0通关率、录入、完成率、正确率、修正率，1复刷完成率
                                                   @RequestParam(value = "moldType", required = false, defaultValue = "1") String moldType, // 1正常通关率、2到勤通关率
                                                   @RequestParam(value = "examSchedulingtType", required = false, defaultValue = "0") String examSchedulingtType //  0全部 1已过刷题排班时间 2未到刷题排班时间
    ) {


        if (name == null || name.isEmpty()) {
            name = null;
        }

        Quarter quarter = iQuarterService.getNowQuarter();
        String readingStatusStr = "1,5";//结课的
        String isBeforQuarter = "1";//是否之前学期1是，0否
        if (quarter != null) {
            //当前学期
            if (yearPart.equals(quarter.getYearPart().toString()) && quarterNum.equals(quarter.getNum().toString())) {
                isBeforQuarter = "0";
                readingStatusStr = "1,5";
            }
        }

        Integer isIncludeFirstCourse = 0;
        if (quarter.getId() >= 18) {
            isIncludeFirstCourse = 1;
        }

        if (readingType.equals("2")) {
            readingStatusStr = "1,3,4,5";
        }
        if (StringUtils.isBlank(passDate)) {
            passDate = LocalDate.now().toString();
        }
        if (StringUtils.isBlank(passDateEnd)) {
            passDateEnd = LocalDate.now().toString();
        }

        String rightNumStartDate = "";
        String rightNumEndDate = "";
        if (rightNumStartDateGet != null && !rightNumStartDateGet.isEmpty()) {
            rightNumStartDate = rightNumStartDateGet + " 00:00:00";
        }
        if (rightNumEndDateGet != null && !rightNumEndDateGet.isEmpty()) {
            rightNumEndDate = rightNumEndDateGet + " 23:59:59";
        }

        FenAdminData fenAdminData = iAuthAdminContentService.getAdminData(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));

        AuthAdminContent authAdminContent = iAuthAdminContentService.getPermissionForTeacherId(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), fenAdminData.getAdminId(), fenAdminData.getBaseSchoolIdStr());
        List<MathChaPlan> mathChaPlanList = new ArrayList<>();
        //个人
        if (authAdminContent.getIsPerson() == 1) {
            //如果type为1或者为2
            if (!type.equals("1") && !type.equals("2")) {
                return null;
            }
        }
        //不包含数学科目
        if (StringUtils.isBlank(authAdminContent.getSubjectsIds())) {
            return null;
        }
        if (!authAdminContent.getSubjectsIds().contains("2")) {
            return null;
        }


        String schoolIdStr = schoolId;
        if (StringUtils.isBlank(schoolId)) {
            //校区老师状态，只能看自己所在校区的数据
            schoolIdStr = authAdminContent.getSchoolIds();
        } else {
            schoolIdStr = iAuthAdminContentService.getIntersectionForStr(schoolId, authAdminContent.getSchoolIds());
            String leadSchoolIds = iAuthAdminContentService.getIntersectionForStr(schoolId, authAdminContent.getLeadDataRs().getSchoolIds());
            authAdminContent.getLeadDataRs().setSchoolIds(leadSchoolIds);
        }

        StringBuilder yearClassIdSb = new StringBuilder();
        String sectionYearClassId = "";
        if (StringUtils.isNotBlank(gradeSection)) {
            String[] sections = gradeSection.split(",");
            for (String section : sections) {
                if (section.equals("0")) {
                    yearClassIdSb.append("1,2");
                } else if (section.equals("1")) {
                    yearClassIdSb.append("3,4,5,6");
                } else if (section.equals("2")) {
                    yearClassIdSb.append("7,8,9");
                } else if (section.equals("3")) {
                    yearClassIdSb.append("10,11,12");
                }
                if (!section.equals(sections[sections.length - 1])) {
                    yearClassIdSb.append(",");
                }
            }
            sectionYearClassId = yearClassIdSb.toString();
            if (StringUtils.isBlank(yearClassId)) {
                yearClassId = authAdminContent.getYearClassIds();
                if (StringUtils.isNotBlank(yearClassId) && StringUtils.isNotBlank(sectionYearClassId)) {
                    yearClassId = CommonUtil.getStrIntersection(yearClassId, sectionYearClassId);
                }
            }
        }

        if (StringUtils.isBlank(yearClassId)) {
            yearClassId = authAdminContent.getYearClassIds();
        } else {
            String leadYearClassIds = iAuthAdminContentService.getIntersectionForStr(yearClassId, authAdminContent.getLeadDataRs().getYearClassIds());
            authAdminContent.getLeadDataRs().setYearClassIds(leadYearClassIds);
            yearClassId = iAuthAdminContentService.getIntersectionForStr(yearClassId, authAdminContent.getYearClassIds());
        }
        if (StringUtils.isBlank(teacherId)) {
            if (authAdminContent.getIsPerson() == 1) {
                teacherId = String.valueOf(this.adminId);
            }
        }
        if (StringUtils.isBlank(subjectsId)) {
            subjectsId = authAdminContent.getSubjectsIds();
        } else {
            String leadSubjectsIds = iAuthAdminContentService.getIntersectionForStr(subjectsId, authAdminContent.getLeadDataRs().getSubjectsIds());
            authAdminContent.getLeadDataRs().setSubjectsIds(leadSubjectsIds);
            subjectsId = iAuthAdminContentService.getIntersectionForStr(subjectsId, authAdminContent.getSubjectsIds());
        }

        Quarter quarterForYearAndNum = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        StringBuilder courseSchedulingDetailName = new StringBuilder();
        courseSchedulingDetailName.append("course_scheduling_detail");
        if (quarter.getId() != quarterForYearAndNum.getId()) {
            courseSchedulingDetailName.append("_");
            courseSchedulingDetailName.append(yearPart);
            courseSchedulingDetailName.append("_");
            courseSchedulingDetailName.append(quarterNum);
        }
        LeadDataRs leadDataRs = authAdminContent.getLeadDataRs();
        String leadSubjectsIds = leadDataRs.getSubjectsIds();
        String leadSchoolIds = leadDataRs.getSchoolIds();
        String leadYearClassIds = leadDataRs.getYearClassIds();
        Long leadTeacherId = leadDataRs.getTeacherId();

        mathChaPlanList = iMathematicsFeignClient.getPassRadeList(schoolIdStr, yearPart, quarterNum, type, lessonNumStart, lessonNum, yearClassId, classTypeId, readingType, readingStatusStr, isBeforQuarter, passDate, passDateStart, passDateEnd, subjectsId, rightNumStartDate, rightNumEndDate, trackId, isYearClass, radeType, listState, teacherId, name, classify, trackType, teamId, include, inType, categoryId, isAchieve, moldType, String.valueOf(courseSchedulingDetailName), examSchedulingtType, leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId, isIncludeFirstCourse);


        if (mathChaPlanList == null || mathChaPlanList.size() == 0) {
            return null;
        }

        if (type.equals("6")) {
            List<MathChaPlan> collectList = new ArrayList<>();

            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("year_part", yearPart);
            queryWrapper.eq("quarter_num", quarterNum);
            queryWrapper.inSql(StringUtils.isNotBlank(schoolId), "school_id", schoolIdStr);
            queryWrapper.eq("pid", 0);
            List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

            for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                MathChaPlan mathChaPlan = new MathChaPlan();
                mathChaPlan.setSchoolId(schoolQuarter.getSchoolId());
                mathChaPlan.setSchoolName(schoolQuarter.getSchoolName());
                mathChaPlan.setSchoolPid(schoolQuarter.getPid());
                mathChaPlan.setAllShutNum(0);
                mathChaPlan.setPlanShutNumPass(0);
                mathChaPlan.setNewNumAll(0);
                mathChaPlan.setLowestShutnumAll(0);
                mathChaPlan.setRightNumAll(0);
                mathChaPlan.setRightNumDate(0);
                mathChaPlan.setAmendNumAll(0);
                mathChaPlan.setErrorNumAll(0);
                mathChaPlan.setShowPassNum(0);
                mathChaPlan.setYetPassNum(0);
                mathChaPlan.setNotPassNum(0);
                mathChaPlan.setNotBrushNum(0);
                mathChaPlan.setAgainBrushNum(0);
                mathChaPlan.setDatePassNum(0);
                mathChaPlan.setYetFinishCount(0);
                mathChaPlan.setDateYetFinishCount(0);
                mathChaPlan.setRebrushCount(0);
                mathChaPlan.setYetRebrushCount(0);
                mathChaPlan.setDateYetRebrushCount(0);
                mathChaPlan.setAgainAmendNum(0);
                mathChaPlan.setYetFinishCount2(0);
                mathChaPlan.setDateYetFinishCount2(0);
                mathChaPlan.setAgainAmendNum2(0);
                mathChaPlan.setAgainBrushNum2(0);
                mathChaPlan.setNotAgainBrushCount(0);
                mathChaPlan.setYetAgainBrushCount(0);
                mathChaPlan.setDateYetAgainBrushCount(0);
                collectList.add(mathChaPlan);
            }

            for (MathChaPlan mathChaPlan : collectList) {
                List<MathChaPlan> childRates = new ArrayList<>();
                for (MathChaPlan chaPlan : mathChaPlanList) {
                    if (mathChaPlan.getSchoolId().equals(chaPlan.getSchoolId()) || mathChaPlan.getSchoolId().equals(chaPlan.getSchoolPid())) {
                        childRates.add(chaPlan);
                        if (chaPlan.getAllShutNum() != null) {
                            mathChaPlan.setAllShutNum(mathChaPlan.getAllShutNum() + chaPlan.getAllShutNum());
                        }
                        if (chaPlan.getPlanShutNumPass() != null) {
                            mathChaPlan.setPlanShutNumPass(mathChaPlan.getPlanShutNumPass() + chaPlan.getPlanShutNumPass());
                        }
                        if (chaPlan.getNewNumAll() != null) {
                            mathChaPlan.setNewNumAll(mathChaPlan.getNewNumAll() + chaPlan.getNewNumAll());
                        }
                        if (chaPlan.getLowestShutnumAll() != null) {
                            mathChaPlan.setLowestShutnumAll(mathChaPlan.getLowestShutnumAll() + chaPlan.getLowestShutnumAll());
                        }
                        if (chaPlan.getRightNumAll() != null) {
                            mathChaPlan.setRightNumAll(mathChaPlan.getRightNumAll() + chaPlan.getRightNumAll());
                        }
                        if (chaPlan.getRightNumDate() != null) {
                            mathChaPlan.setRightNumDate(mathChaPlan.getRightNumDate() + chaPlan.getRightNumDate());
                        }
                        if (chaPlan.getAmendNumAll() != null) {
                            mathChaPlan.setAmendNumAll(mathChaPlan.getAmendNumAll() + chaPlan.getAmendNumAll());
                        }
                        if (chaPlan.getErrorNumAll() != null) {
                            mathChaPlan.setErrorNumAll(mathChaPlan.getErrorNumAll() + chaPlan.getErrorNumAll());
                        }
                        if (chaPlan.getShowPassNum() != null) {
                            mathChaPlan.setShowPassNum(mathChaPlan.getShowPassNum() + chaPlan.getShowPassNum());
                        }
                        if (chaPlan.getYetPassNum() != null) {
                            mathChaPlan.setYetPassNum(mathChaPlan.getYetPassNum() + chaPlan.getYetPassNum());
                        }
                        if (chaPlan.getNotPassNum() != null) {
                            mathChaPlan.setNotPassNum(mathChaPlan.getNotPassNum() + chaPlan.getNotPassNum());
                        }
                        if (chaPlan.getNotBrushNum() != null) {
                            mathChaPlan.setNotBrushNum(mathChaPlan.getNotBrushNum() + chaPlan.getNotBrushNum());
                        }
                        if (chaPlan.getAgainBrushNum() != null) {
                            mathChaPlan.setAgainBrushNum(mathChaPlan.getAgainBrushNum() + chaPlan.getAgainBrushNum());
                        }
                        if (chaPlan.getDatePassNum() != null) {
                            mathChaPlan.setDatePassNum(mathChaPlan.getDatePassNum() + chaPlan.getDatePassNum());
                        }
                        if (chaPlan.getYetFinishCount() != null) {
                            mathChaPlan.setYetFinishCount(mathChaPlan.getYetFinishCount() + chaPlan.getYetFinishCount());
                        }
                        if (chaPlan.getDateYetFinishCount() != null) {
                            mathChaPlan.setDateYetFinishCount(mathChaPlan.getDateYetFinishCount() + chaPlan.getDateYetFinishCount());
                        }
                        if (chaPlan.getRebrushCount() != null) {
                            mathChaPlan.setRebrushCount(mathChaPlan.getRebrushCount() + chaPlan.getRebrushCount());
                        }
                        if (chaPlan.getYetRebrushCount() != null) {
                            mathChaPlan.setYetRebrushCount(mathChaPlan.getYetRebrushCount() + chaPlan.getYetRebrushCount());
                        }
                        if (chaPlan.getDateYetRebrushCount() != null) {
                            mathChaPlan.setDateYetRebrushCount(mathChaPlan.getDateYetRebrushCount() + chaPlan.getDateYetRebrushCount());
                        }
                        if (chaPlan.getAgainAmendNum() != null) {
                            mathChaPlan.setAgainAmendNum(mathChaPlan.getAgainAmendNum() + chaPlan.getAgainAmendNum());
                        }
                        if (chaPlan.getYetFinishCount2() != null) {
                            mathChaPlan.setYetFinishCount2(mathChaPlan.getYetFinishCount2() + chaPlan.getYetFinishCount2());
                        }
                        if (chaPlan.getDateYetFinishCount2() != null) {
                            mathChaPlan.setDateYetFinishCount2(mathChaPlan.getDateYetFinishCount2() + chaPlan.getDateYetFinishCount2());
                        }
                        if (chaPlan.getAgainAmendNum2() != null) {
                            mathChaPlan.setAgainAmendNum2(mathChaPlan.getAgainAmendNum2() + chaPlan.getAgainAmendNum2());
                        }
                        if (chaPlan.getAgainBrushNum2() != null) {
                            mathChaPlan.setAgainBrushNum2(mathChaPlan.getAgainBrushNum2() + chaPlan.getAgainBrushNum2());
                        }
                        if (chaPlan.getNotAgainBrushCount() != null) {
                            mathChaPlan.setNotAgainBrushCount(mathChaPlan.getNotAgainBrushCount() + chaPlan.getNotAgainBrushCount());
                        }
                        if (chaPlan.getYetAgainBrushCount() != null) {
                            mathChaPlan.setYetAgainBrushCount(mathChaPlan.getYetAgainBrushCount() + chaPlan.getYetAgainBrushCount());
                        }
                        if (chaPlan.getDateYetAgainBrushCount() != null) {
                            mathChaPlan.setDateYetAgainBrushCount(mathChaPlan.getDateYetAgainBrushCount() + chaPlan.getDateYetAgainBrushCount());
                        }
                    }
                }
                mathChaPlan.setChildRateList(childRates);
            }

            mathChaPlanList = collectList;
        }

        for (MathChaPlan mathChaPlan : mathChaPlanList) {
            Staff staff = iStaffService.getById(mathChaPlan.getTeacherId());
            if (staff != null) {
                mathChaPlan.setTeacherName(staff.getName());
            }
            Organization organization = iOrganizationService.getById(mathChaPlan.getSchoolId());
            if (organization != null) {
                mathChaPlan.setSchoolName(organization.getName());
            }
        }

        return mathChaPlanList;
    }


    /**
     * 数学完成率详情
     */
    @ApiMethod(value = "home:math:clearance-rate", description = "数学通关率")
    @GetMapping("/getPassRadeDetail")
    public Result getPassRadeDetail(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "schoolId", required = false) String schoolId,
                                    @RequestParam(value = "yearPart", required = false) String yearPart,
                                    @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                    @RequestParam(value = "teacherId", required = false) String teacherId,
                                    @RequestParam(value = "type", required = false) String type,
                                    @RequestParam(value = "gradeId", required = false) String gradeId,
                                    @RequestParam(value = "lessonNumStart", required = false, defaultValue = "0") String lessonNumStart,
                                    @RequestParam(value = "lessonNum", required = false) String lessonNum,
                                    @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                    @RequestParam(value = "gradeSection", required = false) String gradeSection,
                                    @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                    @RequestParam(value = "readingType", required = false, defaultValue = "1") String readingType,
                                    @RequestParam(value = "passType", required = false) String passType,
                                    @RequestParam(value = "passDate", required = false) String passDate,
                                    @RequestParam(value = "passDateStart", required = false) String passDateStart,
                                    @RequestParam(value = "passDateEnd", required = false) String passDateEnd,
                                    @RequestParam(value = "courseVersionId", required = false) String courseVersionId,
                                    @RequestParam(value = "subjectsId", required = false, defaultValue = "2") String subjectsId,
                                    @RequestParam(value = "rightNumStartDate", required = false) String rightNumStartDateGet,
                                    @RequestParam(value = "rightNumEndDate", required = false) String rightNumEndDateGet,
                                    @RequestParam(value = "studentName", required = false) String studentName,
                                    @RequestParam(value = "radeType", required = false) String radeType,
                                    @RequestParam(value = "include", required = false) String include,//0三次课以内，1三次课以外
                                    @RequestParam(value = "classify", required = false) String classify,
                                    @RequestParam(value = "trackType", required = false) String trackType,
                                    @RequestParam(value = "teamId", required = false) String teamId,
                                    @RequestParam(value = "listState", required = false) String listState,
                                    @RequestParam(value = "studentIdStr", required = false) String studentIdStr,
                                    @RequestParam(value = "inType", required = false, defaultValue = "1") String inType,
                                    @RequestParam(value = "categoryId", defaultValue = "1", required = false) Integer categoryId,
                                    @RequestParam(value = "moldType", required = false, defaultValue = "1") String moldType, // 1正常通关率、2到勤通关率
                                    @RequestParam(value = "examSchedulingtType", required = false, defaultValue = "0") String examSchedulingtType // 0全部 1已过刷题排班时间 2未到刷题排班时间
    ) {

        if (StringUtils.isBlank(radeType)) {
            return new Result(Code.ERROR, "", "更新了页面，请刷新查看");
        }

        if (StringUtils.isBlank(passDate)) {
            passDate = LocalDate.now().toString();
        }
        if (StringUtils.isBlank(passDateEnd)) {
            passDateEnd = LocalDate.now().toString();
        }

        Quarter quarter = iQuarterService.getNowQuarter();
        String readingStatusStr = "1,5";//结课的
        String isBeforQuarter = "1";//是否之前学期1是，0否
        if (quarter != null) {
            //当前学期
            if (yearPart.equals(quarter.getYearPart().toString()) && quarterNum.equals(quarter.getNum().toString())) {
                isBeforQuarter = "0";
                readingStatusStr = "1,5";
            }
        }

        Integer isIncludeFirstCourse = 0;
        if (quarter.getId() >= 18) {
            isIncludeFirstCourse = 1;
        }

        if (readingType.equals("2")) {
            readingStatusStr = "1,3,4,5";
        }

        String schoolIdStr = schoolId;


        AuthAdminContent authAdminContent = iAuthAdminContentService.getPermissionForTeacherId(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), this.adminId, this.baseSchoolIdStr);
        if (StringUtils.isBlank(schoolId)) {
            //校区老师状态，只能看自己所在校区的数据
            schoolIdStr = authAdminContent.getSchoolIds();
        } else {
            String leadSchoolIds = iAuthAdminContentService.getIntersectionForStr(schoolId, authAdminContent.getLeadDataRs().getSchoolIds());
            authAdminContent.getLeadDataRs().setSchoolIds(leadSchoolIds);
            schoolIdStr = iAuthAdminContentService.getIntersectionForStr(schoolId, authAdminContent.getSchoolIds());
        }
        LeadDataRs leadDataRs = authAdminContent.getLeadDataRs();

        if (StringUtils.isNotBlank(teacherId)) {
            if (Long.parseLong(teacherId) != leadDataRs.getTeacherId()) {
                leadDataRs.setTeacherId(0L);//非当前老师
            }
        }

        StringBuilder yearClassIdSb = new StringBuilder();
        String sectionYearClassId = "";
        if (StringUtils.isNotBlank(gradeSection)) {
            String[] sections = gradeSection.split(",");
            for (String section : sections) {
                if (section.equals("0")) {
                    yearClassIdSb.append("1,2");
                } else if (section.equals("1")) {
                    yearClassIdSb.append("3,4,5,6");
                } else if (section.equals("2")) {
                    yearClassIdSb.append("7,8,9");
                } else if (section.equals("3")) {
                    yearClassIdSb.append("10,11,12");
                }
                if (!section.equals(sections[sections.length - 1])) {
                    yearClassIdSb.append(",");
                }
            }
            sectionYearClassId = yearClassIdSb.toString();
            if (StringUtils.isBlank(yearClassId)) {
                yearClassId = authAdminContent.getYearClassIds();
                if (StringUtils.isNotBlank(yearClassId) && StringUtils.isNotBlank(sectionYearClassId)) {
                    yearClassId = CommonUtil.getStrIntersection(yearClassId, sectionYearClassId);
                }
            }
        }

        if (StringUtils.isBlank(yearClassId)) {
            yearClassId = authAdminContent.getYearClassIds();
        } else {
            String leadYearClassIds = iAuthAdminContentService.getIntersectionForStr(yearClassId, leadDataRs.getYearClassIds());
            leadDataRs.setYearClassIds(leadYearClassIds);
            yearClassId = iAuthAdminContentService.getIntersectionForStr(yearClassId, authAdminContent.getYearClassIds());
        }
        if (StringUtils.isNotBlank(yearClassId) && StringUtils.isNotBlank(sectionYearClassId)) {
            yearClassId = CommonUtil.getStrIntersection(yearClassId,sectionYearClassId);
        }

        String leadSubjectsIds = leadDataRs.getSubjectsIds();
        String leadSchoolIds = leadDataRs.getSchoolIds();
        String leadYearClassIds = leadDataRs.getYearClassIds();
        Long leadTeacherId = leadDataRs.getTeacherId();


        String rightNumStartDate = "";
        String rightNumEndDate = "";
        if (rightNumStartDateGet != null && !rightNumStartDateGet.isEmpty()) {
            rightNumStartDate = rightNumStartDateGet + " 00:00:00";
        }
        if (rightNumEndDateGet != null && !rightNumEndDateGet.isEmpty()) {
            rightNumEndDate = rightNumEndDateGet + " 23:59:59";
        }

        Quarter quarterForYearAndNum = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        StringBuilder courseSchedulingDetailName = new StringBuilder();
        courseSchedulingDetailName.append("course_scheduling_detail");
        if (quarter.getId() != quarterForYearAndNum.getId()) {
            courseSchedulingDetailName.append("_");
            courseSchedulingDetailName.append(yearPart);
            courseSchedulingDetailName.append("_");
            courseSchedulingDetailName.append(quarterNum);
        }

        Page<MathChaPlan> iPage = iMathematicsFeignClient.getPassRadeDetail(pageNum, pageSize, schoolIdStr, yearPart, quarterNum, teacherId, type, gradeId, lessonNumStart, lessonNum, yearClassId, classTypeId, readingType, readingStatusStr, isBeforQuarter, passType, passDate, passDateStart, passDateEnd, courseVersionId, subjectsId, rightNumStartDate, rightNumEndDate, studentName, radeType, include, classify, trackType, teamId, listState, inType, categoryId, String.valueOf(courseSchedulingDetailName), studentIdStr, moldType, examSchedulingtType, leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId, isIncludeFirstCourse);

        for (MathChaPlan mathChaPlan : iPage.getRecords()) {
            Student student = iStudentService.getById(mathChaPlan.getStudentId());
            if (student != null) {
                mathChaPlan.setStudentName(student.getName());
            }

            //老师姓名
            Staff staff = iStaffService.getById(mathChaPlan.getTeacherId());
            if (staff != null) {
                mathChaPlan.setTeacherName(staff.getName());
            }

            if (type.equals("2") || type.equals("4") || type.equals("6")) {
                Organization organization = iOrganizationService.getById(mathChaPlan.getSchoolId());
                if (organization != null) {
                    mathChaPlan.setSchoolName(organization.getName());
                }
            }

            if (mathChaPlan.getClassTypeId() != null && mathChaPlan.getClassTypeId().equals(1565023758494501025L)) {
                mathChaPlan.setClassTypeName("基础班");
            } else {
                CourseType courseType = iCourseTypeService.getById(mathChaPlan.getClassTypeId());
                if (courseType != null) {
                    mathChaPlan.setClassTypeName(courseType.getName());
                }
            }
            Grade grade = iGradeService.getById(mathChaPlan.getGradeId());
            if (grade != null) {
                mathChaPlan.setGradeName(grade.getName());
            }
            if (listState != null && !listState.isEmpty() && "1".equals(listState)) {
                Integer lessonNumGet = mathChaPlan.getLessonNum();//保留原来的课次
                try {
                    mathChaPlan.setLessonNum(Integer.valueOf(lessonNum));//写进请求参数
                    mathChaPlan.setLessonNumStart(Integer.valueOf(lessonNumStart));//写进请求参数
                    mathChaPlan.setInclude(include);
                    mathChaPlan.setPassType(Integer.valueOf(passType));
                    mathChaPlan.setPassDateStart(passDateStart);
                    mathChaPlan.setPassDateEnd(passDateEnd);
                    mathChaPlan.setTaskCategoryId(categoryId);
                    String chaPlanLessonNumStringEs = iMathematicsFeignClient.getChaPlanLessonNumString(mathChaPlan);
                    String output = uniqueConcatComma(chaPlanLessonNumStringEs);
                    mathChaPlan.setLessonNumNot(output);
                } catch (NumberFormatException ignored) {
                }
                mathChaPlan.setLessonNum(lessonNumGet);
            }
        }


        MathChaPlan mathChaPlan = iMathematicsFeignClient.getPassRadeDetailTotal(schoolIdStr, yearPart, quarterNum, teacherId, type, gradeId, lessonNumStart, lessonNum, yearClassId, classTypeId, readingType, readingStatusStr, isBeforQuarter, passType, passDate, passDateStart, passDateEnd, courseVersionId, subjectsId, rightNumStartDate, rightNumEndDate, studentName, radeType, include, inType, categoryId);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages(), mathChaPlan));
    }

//    @ApiOperation("正确率详情导出")
//    @GetMapping("/getPassRadeDetailExcel")
//    public Result getPassRadeDetailExcel(@RequestParam(value = "schoolId", required = false) String schoolId,
//                                         @RequestParam(value = "yearPart", required = false) String yearPart,
//                                         @RequestParam(value = "quarterNum", required = false) String quarterNum,
//                                         @RequestParam(value = "teacherId", required = false) String teacherId,
//                                         @RequestParam(value = "type", required = false) String type,
//                                         @RequestParam(value = "gradeId", required = false) String gradeId,
//                                         @RequestParam(value = "lessonNumStart", required = false, defaultValue = "0") String lessonNumStart,
//                                         @RequestParam(value = "lessonNum", required = false) String lessonNum,
//                                         @RequestParam(value = "yearClassId", required = false) String yearClassId,
//                                         @RequestParam(value = "gradeSection", required = false) String gradeSection,
//                                         @RequestParam(value = "classTypeId", required = false) String classTypeId,
//                                         @RequestParam(value = "readingType", required = false, defaultValue = "1") String readingType,
//                                         @RequestParam(value = "passType", required = false) String passType,
//                                         @RequestParam(value = "passDate", required = false) String passDate,
//                                         @RequestParam(value = "passDateStart", required = false) String passDateStart,
//                                         @RequestParam(value = "passDateEnd", required = false) String passDateEnd,
//                                         @RequestParam(value = "courseVersionId", required = false) String courseVersionId,
//                                         @RequestParam(value = "subjectsId", required = false, defaultValue = "2") String subjectsId,
//                                         @RequestParam(value = "rightNumStartDate", required = false) String rightNumStartDateGet,
//                                         @RequestParam(value = "rightNumEndDate", required = false) String rightNumEndDateGet,
//                                         @RequestParam(value = "studentName", required = false) String studentName,
//                                         @RequestParam(value = "radeType", required = false) String radeType,
//                                         @RequestParam(value = "include", required = false) String include,//0三次课以内，1三次课以外
//                                         @RequestParam(value = "listState", required = false) String listState) throws IOException {
//        if (StringUtils.isBlank(passDate)) {
//            passDate = LocalDate.now().toString();
//        }
//        if (StringUtils.isBlank(passDateEnd)) {
//            passDateEnd = LocalDate.now().toString();
//        }
//
//        Quarter quarter = iQuarterService.getNowQuarter();
//        String readingStatusStr = "1,5";//结课的
//        String isBeforQuarter = "1";//是否之前学期1是，0否
//        if (quarter != null) {
//            //当前学期
//            if (yearPart.equals(quarter.getYearPart().toString()) && quarterNum.equals(quarter.getNum().toString())) {
//                isBeforQuarter = "0";
//                readingStatusStr = "1,5";
//            }
//        }
//        if (readingType.equals("2")) {
//            readingStatusStr = "1,3,4,5";
//        }
//
//        if (StringUtils.isNotBlank(gradeSection)) {
//            yearClassId = "";
//            if (gradeSection.contains("0")) {
//                yearClassId += "0,1,2";
//            }
//            if (gradeSection.contains("1")) {
//                if (!yearClassId.isEmpty()) {
//                    yearClassId += ",";
//                }
//                yearClassId += "3,4,5,6";
//            }
//            if (gradeSection.contains("2")) {
//                if (!yearClassId.isEmpty()) {
//                    yearClassId += ",";
//                }
//                yearClassId += "7,8,9";
//            }
//            if (gradeSection.contains("3")) {
//                if (!yearClassId.isEmpty()) {
//                    yearClassId += ",";
//                }
//                yearClassId += "10,11,12";
//            }
//        }
//
//        String rightNumStartDate = "";
//        String rightNumEndDate = "";
//        if (rightNumStartDateGet != null && !rightNumStartDateGet.isEmpty()) {
//            rightNumStartDate = rightNumStartDateGet + " 00:00:00";
//        }
//        if (rightNumEndDateGet != null && !rightNumEndDateGet.isEmpty()) {
//            rightNumEndDate = rightNumEndDateGet + " 23:59:59";
//        }
//
//        Page<MathChaPlan> iPage = iMathematicsFeignClient.getPassRadeDetail(-1, -1, schoolId, yearPart, quarterNum, teacherId, type, gradeId, lessonNumStart, lessonNum, yearClassId, classTypeId, readingType, readingStatusStr, isBeforQuarter, passType, passDate, passDateStart, passDateEnd, courseVersionId, subjectsId, rightNumStartDate, rightNumEndDate, studentName, radeType, include, listState);
//
//        for (MathChaPlan mathChaPlan : iPage.getRecords()) {
//            Student student = iStudentService.getById(mathChaPlan.getStudentId());
//            if (student != null) {
//                mathChaPlan.setStudentName(student.getName());
//            }
//            if (mathChaPlan.getClassTypeId() != null && mathChaPlan.getClassTypeId().equals(1565023758494501025L)) {
//                mathChaPlan.setClassTypeName("基础班");
//            } else {
//                CourseType courseType = iCourseTypeService.getById(mathChaPlan.getClassTypeId());
//                if (courseType != null) {
//                    mathChaPlan.setClassTypeName(courseType.getName());
//                }
//            }
//            Grade grade = iGradeService.getById(mathChaPlan.getGradeId());
//            if (grade != null) {
//                mathChaPlan.setGradeName(grade.getName());
//            }
//            if (listState != null && !listState.isEmpty() && "1".equals(listState)) {
//                Integer lessonNumGet = mathChaPlan.getLessonNum();//保留原来的课次
//                try {
//                    mathChaPlan.setLessonNum(Integer.valueOf(lessonNum));//写进请求参数
//                    mathChaPlan.setLessonNumStart(Integer.valueOf(lessonNumStart));//写进请求参数
//                    mathChaPlan.setInclude(include);
//                    mathChaPlan.setPassType(Integer.valueOf(passType));
//                    mathChaPlan.setPassDateStart(passDateStart);
//                    mathChaPlan.setPassDateEnd(passDateEnd);
//                    String chaPlanLessonNumStringEs = iMathematicsFeignClient.getChaPlanLessonNumString(mathChaPlan);
//                    String output = uniqueConcatComma(chaPlanLessonNumStringEs);
//                    mathChaPlan.setLessonNumNot(output);
//                } catch (NumberFormatException ignored) {
//                }
//                mathChaPlan.setLessonNum(lessonNumGet);
//            }
//        }
//
//        List<String> headList = new ArrayList<>();
//        headList.add("学员");
//        headList.add("班型");
//        headList.add("年级");
//        headList.add("课次");
//        headList.add("应通关");
//        headList.add("正确");
//        headList.add("错误");
//        headList.add("正确率");
//
//        Workbook workbook = new HSSFWorkbook();
//        Sheet sheet = workbook.createSheet("正确率详情");
//
//        Font headerFont = workbook.createFont();
//        headerFont.setFontHeightInPoints((short) 14);
//        headerFont.setBold(true);// 设置加粗
//        CellStyle headerCellStyle = workbook.createCellStyle();
//        headerCellStyle.setFont(headerFont);
//        headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
//        headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//        Row headerRow = sheet.createRow(0);
//        sheet.createFreezePane(0, 1);// 冻结首行
//
//        for (int i = 0; i < headList.size(); i++) {
//            Cell cell = headerRow.createCell(i);
//            cell.setCellValue(headList.get(i));
//            cell.setCellStyle(headerCellStyle);
//        }
//
//        Font font = workbook.createFont();
//        font.setFontHeightInPoints((short) 12);
//        CellStyle cellStyle = workbook.createCellStyle();
//        cellStyle.setFont(font);
//        cellStyle.setAlignment(HorizontalAlignment.CENTER);
//        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//
//        int rowIndex = 1; // 数据行起始索引
//        String lastGradeExcel = null; // 上一个学员名称
//        int lastGradeNameRowIndex = 1; // 上一个学员的数据行索引，初始化为1
//
//        BigDecimal scoreAll = new BigDecimal(0.00);//正确率合计
//        Integer rightAll = 0;//正确数合计
//        Integer amendAll = 0;//错误数合计
//        Integer lowestShutAll = 0;//应通关合计
//
//        for (MathChaPlan mathChaPlan : iPage.getRecords()) {
//            Row row = sheet.createRow(rowIndex);
//
//            Cell cell = row.createCell(0);
//            cell.setCellValue(mathChaPlan.getStudentName());
//            sheet.setColumnWidth(0, 20 * 256);
//            cell.setCellStyle(cellStyle);
//
//            Cell cell1 = row.createCell(1);
//            cell1.setCellValue(mathChaPlan.getClassTypeName());
//            sheet.setColumnWidth(1, 20 * 256);
//            cell1.setCellStyle(cellStyle);
//
//            Cell cell2 = row.createCell(2);
//            if (mathChaPlan.getYearClassId() != null) {
//                cell2.setCellValue(CommonUtil.getYearClassNameForId(mathChaPlan.getYearClassId()));
//            }
//            sheet.setColumnWidth(2, 20 * 256);
//            cell2.setCellStyle(cellStyle);
//
//            Cell cell3 = row.createCell(3);
//            cell3.setCellValue(mathChaPlan.getLessonNum());
//            sheet.setColumnWidth(3, 15 * 256);
//            cell3.setCellStyle(cellStyle);
//
//            Cell cell4 = row.createCell(4);
//            cell4.setCellValue(mathChaPlan.getLowestShutnum());
//            lowestShutAll += mathChaPlan.getLowestShutnum();
//            sheet.setColumnWidth(4, 15 * 256);
//            cell4.setCellStyle(cellStyle);
//
//            Cell cell5 = row.createCell(5);
//            cell5.setCellValue(mathChaPlan.getRightNum());
//            rightAll += mathChaPlan.getRightNum();
//            sheet.setColumnWidth(5, 15 * 256);
//            cell5.setCellStyle(cellStyle);
//
//            Cell cell6 = row.createCell(6);
//            cell6.setCellValue(mathChaPlan.getRightNum());
//            amendAll += mathChaPlan.getRightNum();
//            sheet.setColumnWidth(6, 15 * 256);
//            cell6.setCellStyle(cellStyle);
//
//            Cell cell7 = row.createCell(7);
//            if (mathChaPlan.getLowestShutnum() != null && mathChaPlan.getRightNum() != null) {
//                BigDecimal score = BigDecimal.valueOf(mathChaPlan.getRightNum())
//                        .divide(BigDecimal.valueOf(mathChaPlan.getLowestShutnum()), 4, BigDecimal.ROUND_HALF_UP)
//                        .multiply(BigDecimal.valueOf(100));
//                score = score.setScale(2, BigDecimal.ROUND_HALF_UP);
//                scoreAll.add(score);//该学员的正确率合计
//                cell7.setCellValue(score + "%");
//            }
//            sheet.setColumnWidth(7, 20 * 256);
//            cell7.setCellStyle(cellStyle);
//
//            // 学员姓名~班型合并逻辑
//            if (lastGradeExcel != null && !mathChaPlan.getStudentName().equals(lastGradeExcel)) {
//                if (rowIndex - lastGradeNameRowIndex > 1) {
//                    for (int j = 0; j <= 2; j++) {
//                        sheet.addMergedRegion(new CellRangeAddress(lastGradeNameRowIndex, rowIndex - 1, j, j));
//                    }
//                }
//                lastGradeNameRowIndex = rowIndex; // 更新为当前数据行索引
//            }
//            lastGradeExcel = mathChaPlan.getStudentName();
//
//
//            rowIndex++;
//        }
//
//        // 处理最后一个学员的合并
//        if (rowIndex - lastGradeNameRowIndex > 1) {
//            for (int j = 0; j <= 2; j++) {
//                sheet.addMergedRegion(new CellRangeAddress(lastGradeNameRowIndex, rowIndex - 1, j, j));
//            }
//        }
//
//        String fileName = "正确率详情" + ".xls";
//        String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
//        response.setContentType("application/zip");//设置为zip格式
//        response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName);//设置响应头
//
//        // 获取响应输出流
//        OutputStream outputStream = response.getOutputStream();
//
//        // 将工作簿写入输出流
//        workbook.write(outputStream);
//
//        // 关闭工作簿
//        workbook.close();
//
//        // 刷新输出流
//        outputStream.flush();
//
//        //关闭输出流
//        outputStream.close();
//
//        return new Result(Code.OK, null, "导出成功");
//    }


    private String[] removeDuplicates(String[] array) {
        Set<String> uniqueSet = new HashSet<>();

        for (String str : array) {
            uniqueSet.add(str);
        }

        // 将Set转换回数组
        return uniqueSet.toArray(new String[0]);
    }

    private String uniqueConcatComma(String input) {
        if (StringUtils.isEmpty(input)) {
            return "";
        }
        // 使用逗号分割字符串，并移除重复项
        Set<String> uniqueItems = new HashSet<>(Arrays.asList(input.split(",")));
        Set<Integer> intSet = new TreeSet<>();

        for (String str : uniqueItems) {
            intSet.add(Integer.parseInt(str));
        }

        // 将集合重新拼接为字符串，并用逗号分隔
        return intSet.stream()
                .map(Object::toString)
                .collect(Collectors.joining(","));
    }

    @GetMapping("/getTaskForNowList")
    public Result getTaskForNowList(@RequestParam("yearPart") String yearPart,
                                    @RequestParam("quarterNum") String quarterNum,
                                    @RequestParam(value = "subjectsId", required = false, defaultValue = "2") String subjectsId,
                                    @RequestParam(value = "categoryId", defaultValue = "1", required = false) Integer categoryId) {
        Map<String, Object> objectMap = iMathematicsFeignClient.getTaskForNowList(yearPart, quarterNum, subjectsId, categoryId);
        return new Result(Code.OK, objectMap, "");
    }

    /**
     * 荣誉榜单列表
     */
    @GetMapping("/getHonorList")
    public Result getHonorList(@RequestParam(value = "schoolId", required = false) String schoolId,
                               @RequestParam(value = "yearPart", required = false) String yearPart,
                               @RequestParam(value = "quarterNum", required = false) String quarterNum,
                               @RequestParam(value = "type", required = false) String type,
                               @RequestParam(value = "lessonNumStart", required = false, defaultValue = "0") String lessonNumStart,
                               @RequestParam(value = "lessonNumEnd", required = false) String lessonNumEnd,
                               @RequestParam(value = "yearClassId", required = false) String yearClassId,
                               @RequestParam(value = "gradeSection", required = false) String gradeSection,
                               @RequestParam(value = "classTypeId", required = false) String classTypeId,
                               @RequestParam(value = "readingType", required = false, defaultValue = "1") String readingType,
                               @RequestParam(value = "subjectsId", required = false, defaultValue = "2") String subjectsId,
                               @RequestParam(value = "trackId", required = false) String trackId,
                               @RequestParam(value = "radeType", required = false) String radeType,
                               @RequestParam(value = "teacherId", required = false) String teacherId,
                               @RequestParam(value = "name", required = false) String name,
                               @RequestParam(value = "classify", required = false) String classify,
                               @RequestParam(value = "trackType", required = false) String trackType,
                               @RequestParam(value = "teamId", required = false) String teamId,
                               @RequestParam(value = "include", required = false) String include,//0三次课以内，1三次课以外
                               @RequestParam(value = "taskCategoryId", defaultValue = "1", required = false) Integer taskCategoryId,// 1正式，3考前冲刺
                               @RequestParam(value = "startDate", required = false) String startDate,
                               @RequestParam(value = "endDate", required = false) String endDate
    ) {

        if (name == null || name.isEmpty()) {
            name = null;
        }

        if (StringUtils.isNotBlank(gradeSection)) {
            yearClassId = "";
            if (gradeSection.contains("0")) {
                yearClassId += "0,1,2";
            }
            if (gradeSection.contains("1")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "3,4,5,6";
            }
            if (gradeSection.contains("2")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "7,8,9";
            }
            if (gradeSection.contains("3")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "10,11,12";
            }
        }

        if (StringUtils.isBlank(radeType)) {
            return new Result(Code.ERROR, "", "更新了页面，请刷新查看");
        }

        Quarter quarter = iQuarterService.getNowQuarter();
        String isBeforQuarter = "1";//是否之前学期1是，0否
        if (quarter != null) {
            //当前学期
            if (yearPart.equals(quarter.getYearPart().toString()) && quarterNum.equals(quarter.getNum().toString())) {
                isBeforQuarter = "0";
            }
        }

        if (StringUtils.isBlank(yearPart) || StringUtils.isBlank(quarterNum)) {
            yearPart = iQuarterService.getNowQuarter().getYearPart().toString();
            quarterNum = iQuarterService.getNowQuarter().getNum().toString();
        }

        String schoolIdStr = schoolId;
        if (StringUtils.isBlank(schoolId)) {
            if (type.equals("2")) {
                //校区老师状态，只能看自己所在校区的数据
                schoolIdStr = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
            }
        }

        List<MathChaPlan> mathChaPlanList = iMathematicsFeignClient.getHonorList(yearPart, quarterNum, schoolIdStr, yearClassId, type, lessonNumStart, lessonNumEnd, classTypeId, readingType, subjectsId, isBeforQuarter, trackId, teacherId, name, include, taskCategoryId, classify, trackType, teamId, startDate, endDate);

        if (mathChaPlanList == null || mathChaPlanList.size() == 0) {
            return new Result(Code.OK, new PageResult<>(new ArrayList<>(), 0L, 1L), "无数据");
        }

        if (type.equals("6") && StringUtils.isBlank(schoolId)) {
            List<MathChaPlan> collectList = new ArrayList<>();

            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("year_part", yearPart);
            queryWrapper.eq("quarter_num", quarterNum);
            queryWrapper.eq("pid", 0);
            List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

            for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                MathChaPlan mathChaPlan = new MathChaPlan();
                mathChaPlan.setSchoolId(schoolQuarter.getSchoolId());
                mathChaPlan.setSchoolName(schoolQuarter.getSchoolName());
                mathChaPlan.setSchoolPid(schoolQuarter.getPid());
                mathChaPlan.setShowPassNum(0);
                mathChaPlan.setOrdinaryNum(0);
                mathChaPlan.setProgressNum(0);
                mathChaPlan.setExcellentNum(0);
                mathChaPlan.setApexNum(0);
                collectList.add(mathChaPlan);
            }

            for (MathChaPlan mathChaPlan : collectList) {
                List<MathChaPlan> childRates = new ArrayList<>();
                for (MathChaPlan chaPlan : mathChaPlanList) {
                    if (mathChaPlan.getSchoolId().equals(chaPlan.getSchoolId()) || mathChaPlan.getSchoolId().equals(chaPlan.getSchoolPid())) {
                        childRates.add(chaPlan);
                        if (chaPlan.getShowPassNum() != null) {
                            mathChaPlan.setShowPassNum(mathChaPlan.getShowPassNum() + chaPlan.getShowPassNum());
                        }
                        if (chaPlan.getOrdinaryNum() != null) {
                            mathChaPlan.setOrdinaryNum(mathChaPlan.getOrdinaryNum() + chaPlan.getOrdinaryNum());
                        }
                        if (chaPlan.getProgressNum() != null) {
                            mathChaPlan.setProgressNum(mathChaPlan.getProgressNum() + chaPlan.getProgressNum());
                        }
                        if (chaPlan.getExcellentNum() != null) {
                            mathChaPlan.setExcellentNum(mathChaPlan.getExcellentNum() + chaPlan.getExcellentNum());
                        }
                        if (chaPlan.getApexNum() != null) {
                            mathChaPlan.setApexNum(mathChaPlan.getApexNum() + chaPlan.getApexNum());
                        }
                    }
                }
                mathChaPlan.setChildRateList(childRates);
            }
            mathChaPlanList = collectList;
        }

        for (MathChaPlan mathChaPlan : mathChaPlanList) {
            Organization organization = iOrganizationService.getById(mathChaPlan.getSchoolId());
            if (organization != null) {
                mathChaPlan.setSchoolName(organization.getName());
            }
        }

        return new Result(Code.OK, new PageResult<>(mathChaPlanList, (long) mathChaPlanList.size(), 1L));
    }

    /**
     * 荣誉榜单详情
     */
    @GetMapping("/getHonorDetails")
    public Result getHonorDetails(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                  @RequestParam(value = "schoolId", required = false) String schoolId,
                                  @RequestParam(value = "yearPart", required = false) String yearPart,
                                  @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                  @RequestParam(value = "type", required = false) String type,
                                  @RequestParam(value = "lessonNumStart", required = false, defaultValue = "0") String lessonNumStart,
                                  @RequestParam(value = "lessonNumEnd", required = false) String lessonNumEnd,
                                  @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                  @RequestParam(value = "gradeSection", required = false) String gradeSection,
                                  @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                  @RequestParam(value = "gradeId", required = false) String gradeId,
                                  @RequestParam(value = "readingType", required = false, defaultValue = "1") String readingType,
                                  @RequestParam(value = "subjectsId", required = false, defaultValue = "2") String subjectsId,
                                  @RequestParam(value = "trackId", required = false) String trackId,
                                  @RequestParam(value = "radeType", required = false) String radeType,
                                  @RequestParam(value = "teacherId", required = false) String teacherId,
                                  @RequestParam(value = "name", required = false) String name,
                                  @RequestParam(value = "classify", required = false) String classify,
                                  @RequestParam(value = "trackType", required = false) String trackType,
                                  @RequestParam(value = "teamId", required = false) String teamId,
                                  @RequestParam(value = "passType", required = false) String passType,
                                  @RequestParam(value = "include", required = false) String include,//0三次课以内，1三次课以外
                                  @RequestParam(value = "taskCategoryId", defaultValue = "1", required = false) Integer taskCategoryId,// 1正式，3考前冲刺
                                  @RequestParam(value = "startDate", required = false) String startDate,
                                  @RequestParam(value = "endDate", required = false) String endDate
    ) {

        if (name == null || name.isEmpty()) {
            name = null;
        }
        String schoolIdStr = schoolId;
        if (StringUtils.isBlank(schoolId)) {
            //schoolIdStr = this.baseSchoolIdStr;
        }
        if (StringUtils.isNotBlank(gradeSection)) {
            yearClassId = "";
            if (gradeSection.contains("0")) {
                yearClassId += "0,1,2";
            }
            if (gradeSection.contains("1")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "3,4,5,6";
            }
            if (gradeSection.contains("2")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "7,8,9";
            }
            if (gradeSection.contains("3")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "10,11,12";
            }
        }

        if (StringUtils.isBlank(radeType)) {
            return new Result(Code.ERROR, "", "更新了页面，请刷新查看");
        }

        Quarter quarter = iQuarterService.getNowQuarter();
        String isBeforQuarter = "1";//是否之前学期1是，0否
        if (quarter != null) {
            //当前学期
            if (yearPart.equals(quarter.getYearPart().toString()) && quarterNum.equals(quarter.getNum().toString())) {
                isBeforQuarter = "0";
            }
        }

        if (StringUtils.isBlank(yearPart) || StringUtils.isBlank(quarterNum)) {
            yearPart = iQuarterService.getNowQuarter().getYearPart().toString();
            quarterNum = iQuarterService.getNowQuarter().getNum().toString();
        }

        Page<MathChaPlan> iPage = iMathematicsFeignClient.getHonorDetails(pageNum, pageSize, yearPart, quarterNum, schoolIdStr, yearClassId, type, lessonNumStart, lessonNumEnd, classTypeId, readingType, subjectsId, isBeforQuarter, trackId, teacherId, name, include, taskCategoryId, passType, classify, trackType, teamId, gradeId, startDate, endDate);

        for (MathChaPlan mathChaPlan : iPage.getRecords()) {
            if (mathChaPlan.getClassTypeId() != null && mathChaPlan.getClassTypeId().equals(1565023758494501025L)) {
                mathChaPlan.setClassTypeName("基础班");
            } else {
                CourseType courseType = iCourseTypeService.getById(mathChaPlan.getClassTypeId());
                if (courseType != null) {
                    mathChaPlan.setClassTypeName(courseType.getName());
                }
            }
            Student student = iStudentService.getById(mathChaPlan.getStudentId());
            if (student != null) {
                mathChaPlan.setStudentName(student.getName());
            }
        }

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), Code.OK_MSG);
    }

    /**
     * 数学积分列表
     */
    @GetMapping("/getPassRadeIntegralList")
    public Result getPassRadeIntegralList(
            @RequestParam(value = "yearPart", required = false) String yearPart,
            @RequestParam(value = "quarterNum", required = false) String quarterNum,
            @RequestParam(value = "schoolId", required = false) String schoolId,
            @RequestParam(value = "teacherId", required = false) String teacherId,
            @RequestParam(value = "yearClassId", required = false) String yearClassId,
            @RequestParam(value = "gradeSection", required = false) String gradeSection,
            @RequestParam(value = "subjectsId", required = false, defaultValue = "2") Long subjectsId,
            @RequestParam(value = "classTypeId", required = false) String classTypeId,
            @RequestParam(value = "readingType", required = false) String readingType,
            @RequestParam(value = "courseVersionId", required = false, defaultValue = "1676874588501311488") Long courseVersionId,
            @RequestParam(value = "include", required = false) String include,// 0老生，1新生
            @RequestParam(value = "taskCategoryId", required = false, defaultValue = "1") String taskCategoryId,
            @RequestParam(value = "classify", required = false) String classify,
            @RequestParam(value = "trackType", required = false) String trackType,
            @RequestParam(value = "teamId", required = false) String teamId,
            @RequestParam(value = "lessonNumStart", required = false) String lessonNumStart,
            @RequestParam(value = "lessonNumEnd", required = false) String lessonNumEnd,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "inType", required = false) String inType,// 1、全部，2已录
            @RequestParam(value = "moldType", required = false) String moldType,  // 1、正确率，2、通关率，3、费曼学习积分
            @RequestParam(value = "listState", required = false) String listState //1、按班级查看
    ) {
        if (StringUtils.isNotBlank(gradeSection)) {
            yearClassId = "";
            if (gradeSection.contains("0")) {
                yearClassId += "0,1,2";
            }
            if (gradeSection.contains("1")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "3,4,5,6";
            }
            if (gradeSection.contains("2")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "7,8,9";
            }
            if (gradeSection.contains("3")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "10,11,12";
            }
        }

        Quarter quarter = iQuarterService.getNowQuarter();
        String isBeforQuarter = "1";//是否之前学期1是，0否
        if (quarter != null) {
            //当前学期
            if (yearPart.equals(quarter.getYearPart().toString()) && quarterNum.equals(quarter.getNum().toString())) {
                isBeforQuarter = "0";
            }
        }

        String schoolIdStr = schoolId;
        if (StringUtils.isBlank(schoolId)) {
            if (type.equals("2")) {
                //校区老师状态，只能看自己所在校区的数据
                schoolIdStr = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
            }
        }
        List<MathChaPlan> mathChaPlanList = iMathematicsFeignClient.getPassRadeIntegralList(yearPart, quarterNum, schoolIdStr, teacherId, yearClassId, subjectsId, classTypeId, readingType, courseVersionId, include, taskCategoryId, classify, trackType, isBeforQuarter, teamId, lessonNumStart, lessonNumEnd, startDate, endDate, type, inType, moldType, listState);

        if (mathChaPlanList == null || mathChaPlanList.size() == 0) {
            return new Result(Code.OK, new PageResult<>(new ArrayList<>(), 0L, 1L), "无数据");
        }

        if (type.equals("6") && StringUtils.isBlank(schoolId)) {
            List<MathChaPlan> collectList = new ArrayList<>();

            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("year_part", yearPart);
            queryWrapper.eq("quarter_num", quarterNum);
            queryWrapper.eq("pid", 0);
            List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

            for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                MathChaPlan mathChaPlan = new MathChaPlan();
                mathChaPlan.setSchoolId(schoolQuarter.getSchoolId());
                mathChaPlan.setSchoolName(schoolQuarter.getSchoolName());
                mathChaPlan.setSchoolPid(schoolQuarter.getPid());
                mathChaPlan.setIntegralAll(0);
                mathChaPlan.setIntegralAllDate(0);
                collectList.add(mathChaPlan);
            }

            for (MathChaPlan mathChaPlan : collectList) {
                List<MathChaPlan> childRates = new ArrayList<>();
                for (MathChaPlan chaPlan : mathChaPlanList) {
                    if (mathChaPlan.getSchoolId().equals(chaPlan.getSchoolId()) || mathChaPlan.getSchoolId().equals(chaPlan.getSchoolPid())) {
                        childRates.add(chaPlan);
                        if (chaPlan.getIntegralAll() != null) {
                            mathChaPlan.setIntegralAll(mathChaPlan.getIntegralAll() + chaPlan.getIntegralAll());
                        }
                        if (chaPlan.getIntegralAllDate() != null) {
                            mathChaPlan.setIntegralAllDate(mathChaPlan.getIntegralAllDate() + chaPlan.getIntegralAllDate());
                        }
                    }
                }
                mathChaPlan.setChildRateList(childRates);
            }

            mathChaPlanList = collectList;
        }

        for (MathChaPlan mathChaPlan : mathChaPlanList) {
            Organization organization = iOrganizationService.getById(mathChaPlan.getSchoolId());
            if (organization != null) {
                mathChaPlan.setSchoolName(organization.getName());
            }
        }

        return new Result(Code.OK, new PageResult<>(mathChaPlanList, (long) mathChaPlanList.size(), 1L));
    }

    /**
     * 数学积分详情
     */
    @GetMapping("/getPassRadeIntegralDetail")
    public Result getPassRadeIntegralDetail(@RequestParam(value = "pageNum", defaultValue = "-1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "-1") Integer pageSize,
                                            @RequestParam(value = "yearPart", required = false) String yearPart,
                                            @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                            @RequestParam(value = "schoolId", required = false) String schoolId,
                                            @RequestParam(value = "teacherId", required = false) String teacherId,
                                            @RequestParam(value = "studentId", required = false) String studentId,
                                            @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                            @RequestParam(value = "gradeSection", required = false) String gradeSection,
                                            @RequestParam(value = "subjectsId", required = false, defaultValue = "2") Long subjectsId,
                                            @RequestParam(value = "gradeId", required = false) String gradeId,
                                            @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                            @RequestParam(value = "readingType", required = false) String readingType,
                                            @RequestParam(value = "courseVersionId", required = false, defaultValue = "1676874588501311488") Long courseVersionId,
                                            @RequestParam(value = "studentName", required = false) String studentName,
                                            @RequestParam(value = "include", required = false) String include,
                                            @RequestParam(value = "taskCategoryId", required = false, defaultValue = "1") String taskCategoryId,
                                            @RequestParam(value = "classify", required = false) String classify,
                                            @RequestParam(value = "trackType", required = false) String trackType,
                                            @RequestParam(value = "teamId", required = false) String teamId,
                                            @RequestParam(value = "lessonNumStart", required = false) String lessonNumStart,
                                            @RequestParam(value = "lessonNumEnd", required = false) String lessonNumEnd,
                                            @RequestParam(value = "startDate", required = false) String startDate,
                                            @RequestParam(value = "endDate", required = false) String endDate,
                                            @RequestParam(value = "type", required = false) String type,
                                            @RequestParam(value = "listState", required = false) String listState,
                                            @RequestParam(value = "inType", required = false) String inType,// 1、全部，2已录
                                            @RequestParam(value = "moldType", required = false) String moldType  // 1、正确率，2、通关率，3、费曼学习积分
    ) {

        Quarter quarter = iQuarterService.getNowQuarter();
        String isBeforQuarter = "1";//是否之前学期1是，0否
        if (quarter != null) {
            //当前学期
            if (yearPart.equals(quarter.getYearPart().toString()) && quarterNum.equals(quarter.getNum().toString())) {
                isBeforQuarter = "0";
            }
        }

        if (StringUtils.isNotBlank(gradeSection)) {
            yearClassId = "";
            if (gradeSection.contains("0")) {
                yearClassId += "0,1,2";
            }
            if (gradeSection.contains("1")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "3,4,5,6";
            }
            if (gradeSection.contains("2")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "7,8,9";
            }
            if (gradeSection.contains("3")) {
                if (!yearClassId.isEmpty()) {
                    yearClassId += ",";
                }
                yearClassId += "10,11,12";
            }
        }

        Page<MathChaPlan> iPage = iMathematicsFeignClient.getPassRadeIntegralDetail(pageNum, pageSize, yearPart, quarterNum, schoolId, teacherId, studentId, yearClassId, subjectsId, gradeId, classTypeId, readingType, courseVersionId, studentName, include, taskCategoryId, classify, trackType, isBeforQuarter, teamId, lessonNumStart, lessonNumEnd, startDate, endDate, type, inType, moldType, listState);

        for (MathChaPlan mathChaPlan : iPage.getRecords()) {
            if (mathChaPlan.getClassTypeId() != null && mathChaPlan.getClassTypeId().equals(1565023758494501025L)) {
                mathChaPlan.setClassTypeName("基础班");
            } else {
                CourseType courseType = iCourseTypeService.getById(mathChaPlan.getClassTypeId());
                if (courseType != null) {
                    mathChaPlan.setClassTypeName(courseType.getName());
                }
            }
        }
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    /**
     * 数学积分录入列表
     */
    @GetMapping("/getChaPanIntegralAddList")
    public Result getChaPanIntegralAddList(@RequestParam(value = "pageNum", defaultValue = "-1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "-1") Integer pageSize,
                                           @RequestParam(value = "yearPart", required = false) String yearPart,
                                           @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                           @RequestParam(value = "schoolId", required = false) String schoolId,
                                           @RequestParam(value = "teacherId", required = false) String teacherId,
                                           @RequestParam(value = "gradeId", required = false) String gradeId,
                                           @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                           @RequestParam(value = "gradeSection", required = false) String gradeSection,
                                           @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                           @RequestParam(value = "lessonNum", required = false) String lessonNum,
                                           @RequestParam(value = "studentName", required = false) String studentName,
                                           @RequestParam(value = "subjectsId", required = false, defaultValue = "2") Long subjectsId,
                                           @RequestParam(value = "courseVersionId", required = false, defaultValue = "1676874588501311488") Long courseVersionId,
                                           @RequestParam(value = "brushCount", required = false, defaultValue = "1") String brushCount,
                                           @RequestParam(value = "taskCategoryId", required = false, defaultValue = "1") Integer taskCategoryId
    ) {

        String schoolIdStr = schoolId;
        if (StringUtils.isBlank(schoolId)) {
            schoolIdStr = this.baseSchoolIdStr;
        }
        if (StringUtils.isNotBlank(gradeSection)) {
            yearClassId = CommonUtil.getYearClassIdsForGradeSection(Integer.parseInt(gradeSection));
        }

        Page<MathChaPlan> iPage = iMathematicsFeignClient.getChaPanIntegralAddList(pageNum, pageSize, yearPart, quarterNum, schoolIdStr, teacherId, gradeId, yearClassId, classTypeId, lessonNum, studentName, subjectsId, courseVersionId, brushCount, taskCategoryId);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    /**
     * 数学积分录入
     */
    @PostMapping("/addChaPanIntegra")
    public Result addChaPanIntegra(@RequestBody MathChaPlan mathChaPlan) {
        boolean rs = iMathematicsFeignClient.addChaPanIntegra(mathChaPlan);
        if (rs) {
            return new Result(Code.OK, "", "保存成功");
        } else {
            return new Result(Code.ERROR, "", "保存失败");
        }
    }

    @GetMapping("/updatePassLesson")
    public Result updatePassLesson(@RequestParam("yearPart") Integer yearPart,
                                    @RequestParam("quarterNum") Integer quarterNum
    ){
        iMathematicsFeignClient.updatePassLesson(yearPart,quarterNum);
        return new Result(Code.OK, "", "更新中");
    }
}
